package com.mt.mtyx.common.exception;

import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.common.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 *
 * @author WMT
 * @Date 2023/6/12 15:31
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理异常
     *
     * @param e 异常
     * @return
     */
    @ExceptionHandler({Exception.class})
    public Result<Void> handlerSystemError(Exception e) {
        log.error("出现系统异常:{}", e.getMessage());
        e.printStackTrace();
        return Result.fail(ResultCodeEnum.FAIL.getCode(),"出现系统异常");
    }

    /**
     * 处理自定义业务异常
     *
     * @param e 异常
     * @return
     */
    @ExceptionHandler({BusinessException.class})
    public Result<Void> handleBusinessException(BusinessException e) {
        log.error("出现业务异常:{}", e.getMessage());
        e.printStackTrace();
        return Result.fail(e.getCode(), e.getMessage());
    }
}
